package com.h2cloud;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.concurrent.TimeUnit;

/**
 * @author: wulongkai
 * @create: 2022-10-13 15:02
 **/
public class RedisConfig {
    public static void main(String[] args) {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");
        RedissonClient redissonClient = Redisson.create(config);
        String lockKey = "NAME:LOCK";
        RLock lock = redissonClient.getLock(lockKey);   //获取RLock对象
        RSemaphore semaphore = redissonClient.getSemaphore("Semaphore");
        semaphore.trySetPermits(3);
        try {
            semaphore.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        lock.lock();
        try {
            lock.lock(10, TimeUnit.SECONDS); //获取锁,上锁成功以后10秒自动解锁
            System.out.println(Thread.currentThread().getId() + " " + System.currentTimeMillis());

        } finally {
            lock.unlock();  //释放锁, 确保加锁成功后,一定能释放锁
        }
    }

    public void lock() {

    }
}
